﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace M1
{
    public class 摄像机跟随 : MonoBehaviour
    {

        // public Transform target;
        public 人 target;
        public 相机跟随模式 跟随模式;

        public List<Vector3> List观测偏移;
        public Vector3 offsetPos;
        // public Vector3 offsetPos
        // {
        //     get
        //     {
        //         int index = 跟随模式.GetHashCode();
        //         if (listOffsetPos.Count > index)
        //         {
        //             return listOffsetPos[index];
        //         }else if (listOffsetPos.Count > 0)
        //         {
        //             return listOffsetPos[0];
        //         }else
        //         {
        //             return Vector3.zero;
        //         }
        //     }
        // }

        public float moveSpeed = 5;
        public float turnSpeed = 10;
        public float smoothSpeed = 0.5f;

        public bool camRotation; //TODO : If activated during play, an error remains.

        Quaternion targetRotation;
        Vector3 targetPos;
        bool smoothRotating = false;


        void Update()
        {

            if (!camRotation)
            {
                switch (跟随模式)
                {
                    case 相机跟随模式.第三人称:
                        MoveWithTarget();
                        LookAtTarget();
                        break;
                    case 相机跟随模式.选形象:
                        跟随点1();
                        break;
                }
            }
            else
            {
                LookatRotation();
            }

            // if (Input.GetKeyDown(KeyCode.G) && !smoothRotating)
            // {
            //     StartCoroutine("RotateAroundTarget", 45);
            // }
            //
            // if (Input.GetKeyDown(KeyCode.H) && !smoothRotating)
            // {
            //     StartCoroutine("RotateAroundTarget", -45);
            // }
        }

        /*
        void LateUpdate()
        {
            if (camRotation)
            {
                LookatRotation();
                print("LookatRotation");
            }
        }
        */

        void 跟随点1()
        {
            targetPos = target.摄像机点1.position;
            // targetRotation = target.摄像机点1.rotation;
            Vector3 观测偏移 = List观测偏移[相机跟随模式.选形象.GetHashCode()];
            targetRotation = Quaternion.LookRotation(target.transform.position - transform.position+观测偏移);
            
            transform.position = Vector3.Lerp(transform.position, targetPos, moveSpeed * Time.deltaTime);
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
        }
        
        void MoveWithTarget()
        {
            targetPos = target.transform.position + offsetPos;
            transform.position = Vector3.Lerp(transform.position, targetPos, moveSpeed * Time.deltaTime);
        }

        void LookAtTarget()
        {
            targetRotation = Quaternion.LookRotation(target.transform.position - transform.position);
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
        }

        void LookatRotation()
        {
            if (!target) return;

            Transform tran = target.transform;
            float wantedRotationAngle = tran.eulerAngles.y;
            float wantedHeight = tran.position.y + offsetPos.y;

            float currentRotationAngle = transform.eulerAngles.y;
            float currentHeight = transform.position.y;

            currentRotationAngle =
                Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, smoothSpeed * Time.deltaTime);

            currentHeight = Mathf.Lerp(currentHeight, wantedHeight, moveSpeed * Time.deltaTime);

            var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);

            transform.position = tran.position;
            transform.position -= currentRotation * Vector3.forward * -offsetPos.z;

            transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);

            transform.LookAt(tran);
        }

        // IEnumerator RotateAroundTarget(float angle)
        // {
        //     Vector3 vel = Vector3.zero;
        //     Vector3 targerOffsetPos = Quaternion.Euler(0, angle, 0) * offsetPos;
        //     float dist = Vector3.Distance(offsetPos, targerOffsetPos);
        //
        //     smoothRotating = true;
        //
        //     while (dist > 0.02f)
        //     {
        //         offsetPos = Vector3.SmoothDamp(offsetPos, targerOffsetPos, ref vel, smoothSpeed);
        //         dist = Vector3.Distance(offsetPos, targerOffsetPos);
        //         yield return null;
        //     }
        //
        //     smoothRotating = false;
        //     offsetPos = targerOffsetPos;
        // }
    }

    public enum 相机跟随模式
    {
        第三人称,
        选形象,
        第一人称,
    }
}
